package com.dd.common.base

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.dd.common.util.RxUtil
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable

abstract class BaseViewModelAbs<O> : ViewModel(), BaseSubscriber.SubscriberListener {
    private val isShowProgress = MutableLiveData<Boolean>()
    private val noticeMessage = MutableLiveData<NoticeMessage>()
    private var mCompositeSubscription: CompositeDisposable? = null
    private var mRequestCount: Long = 0
    private var context: Context? = null

    fun getIsShowProgress(): LiveData<Boolean> {
        return isShowProgress
    }

    fun getNoticeMessage(): LiveData<NoticeMessage> {
        return noticeMessage
    }

    fun <R : Any> builder(
        observable: Observable<BaseResponse<R>>,
        baseSubscriber: BaseSubscriber<R>
    ) {
        baseSubscriber.setFunctionCallback(object : CommonCallback<Disposable> {
            override fun call(data: Disposable) {
                addSubscribe(data)
            }
        })

        observable.compose(RxUtil.rxSchedulerHelper())
            .compose(RxUtil.handleResult())
            .subscribe(baseSubscriber)
    }

    protected open fun addSubscribe(subscription: Disposable) {
        mCompositeSubscription ?: run {
            mCompositeSubscription = CompositeDisposable()
        }
        mCompositeSubscription?.add(subscription)
    }

    protected abstract fun getApi(): O?

    fun setContext(context: Context) {
        this.context = context
    }

    protected open fun unSubscribe() {
        mCompositeSubscription?.clear()
    }

    /**
     * 请求数量
     * 多请求时需要重写
     * 例：发起三个请求后，隐藏等待框
     *
     * @return
     */
    protected open fun getRequestCount(): Long {
        return 1
    }

    override fun showProgress() {
        isShowProgress.value = true
    }

    override fun hideProgress() {
        if (mRequestCount >= getRequestCount()) {
            isShowProgress.value = false
        }
    }


    override fun showNotice(type: Int, msg: String) {
        noticeMessage.value = NoticeMessage(type, msg)
    }

    override fun onComplete() {
        mRequestCount++
    }

    override fun onCleared() {
        super.onCleared()
        unSubscribe()
    }

    protected open fun <T : Any, K : Any, V : Any> builderZip(
        observable1: Observable<BaseResponse<K>>,
        observable2: Observable<BaseResponse<V>>,
        zipHandleEvent: ZipHandleEvent<T, K, V>,
        baseSubscriber: BaseSubscriber<T>
    ) {
        baseSubscriber.setFunctionCallback(object : CommonCallback<Disposable> {
            override fun call(data: Disposable) {
                addSubscribe(data)
            }
        })
        val ob1: Observable<K> = observable1.compose(RxUtil.rxSchedulerHelper())
            .compose(RxUtil.handleResult())
        val ob2: Observable<V> = observable2.compose(RxUtil.rxSchedulerHelper())
            .compose(RxUtil.handleResult())
        Observable.zip<K, V, T>(ob1, ob2) { tBaseResponse, tBaseResponse2 ->
            zipHandleEvent.handleData(
                tBaseResponse,
                tBaseResponse2
            )
        }.subscribe(baseSubscriber)
    }

    class NoticeMessage(var type: Int, var message: String)
}